import hljs from 'highlight.js';
import 'highlight.js/styles/github.css';
import markdownit from 'markdown-it';

const md = new markdownit({
  highlight: function (str: any, lang: any) {
    if (lang && hljs.getLanguage(lang)) {
      try {
        return (
          '<pre class="code-block-wrapper"><code class="hljs">' +
          hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
          '</code></pre>'
        );
      } catch (__) {}
    }

    return (
      '<pre class="code-block-wrapper"><code class="hljs">' +
      md.utils.escapeHtml(str) +
      '</code></pre>'
    );
  },
});

export const markdown = (text: string) => {
  return md.render(text);
};
